package com.renchi.menu.common.util;


import org.apache.commons.beanutils.*;
import org.apache.commons.beanutils.converters.DateConverter;

import java.beans.PropertyDescriptor;
import java.util.*;
import java.util.Map.Entry;

public class BeanConvertUtils {
    public static ConvertUtilsBean convertUtilsBean;
    private static BeanUtilsBean beanUtilsBean;

    public BeanConvertUtils() {
    }

    public static <T> List<T> convertVoCollection(List collections, Class<T> clazz) {
        if (collections == null) {
            return null;
        } else {
            List<T> vos = new ArrayList();
            collections.forEach((object) -> {
                vos.add(convertToVo(object, clazz));
            });
            return vos;
        }
    }

    public static <T> T convertToVo(Object object, Class<T> clazz) throws RuntimeException {
        if (object == null) {
            return null;
        } else {
            try {
                T t = clazz.newInstance();
                BeanUtils.copyProperties(t, object);
                return t;
            } catch (Exception var3) {
                throw new RuntimeException(var3);
            }
        }
    }

    public static void copyNotNullProperties(Object dest, Object orig) {
        if (dest != null) {
            if (orig != null) {
                try {
                    int i;
                    String name;
                    Object value;
                    if (orig instanceof DynaBean) {
                        DynaProperty[] origDescriptors = ((DynaBean)orig).getDynaClass().getDynaProperties();

                        for(i = 0; i < origDescriptors.length; ++i) {
                            name = origDescriptors[i].getName();
                            if (beanUtilsBean.getPropertyUtils().isReadable(orig, name) && beanUtilsBean.getPropertyUtils().isWriteable(dest, name)) {
                                value = ((DynaBean)orig).get(name);
                                beanUtilsBean.copyProperty(dest, name, value);
                            }
                        }
                    } else if (orig instanceof Map) {
                        Iterator entries = ((Map)orig).entrySet().iterator();

                        while(entries.hasNext()) {
                            Entry entry = (Entry)entries.next();
                            name = (String)entry.getKey();
                            if (beanUtilsBean.getPropertyUtils().isWriteable(dest, name)) {
                                beanUtilsBean.copyProperty(dest, name, entry.getValue());
                            }
                        }
                    } else {
                        PropertyDescriptor[] origDescriptors = beanUtilsBean.getPropertyUtils().getPropertyDescriptors(orig);

                        for(i = 0; i < origDescriptors.length; ++i) {
                            name = origDescriptors[i].getName();
                            if (!"class".equals(name) && beanUtilsBean.getPropertyUtils().isReadable(orig, name) && beanUtilsBean.getPropertyUtils().isWriteable(dest, name)) {
                                try {
                                    value = beanUtilsBean.getPropertyUtils().getSimpleProperty(orig, name);
                                    if (value != null) {
                                        beanUtilsBean.copyProperty(dest, name, value);
                                    }
                                } catch (NoSuchMethodException var6) {
                                    var6.printStackTrace();
                                }
                            }
                        }
                    }
                } catch (Exception var7) {
                    var7.printStackTrace();
                }

            }
        }
    }

    static {
        ConvertUtils.register(new DateConverter((Object)null), Date.class);
        convertUtilsBean = new ConvertUtilsBean();
        beanUtilsBean = new BeanUtilsBean(convertUtilsBean, new PropertyUtilsBean());
    }
}
